package com.qingcloud.adminbackend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingcloud.adminbackend.anno.EventApi;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.message.*;
import com.qingcloud.adminbackend.entity.message.*;
import com.qingcloud.adminbackend.pojo.OptionNode;
import com.qingcloud.adminbackend.service.ISysMessageService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 消息管理 控制类
 * </p>
 *
 * @author zhoujun
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/sys/message/manage")
public class SysMessageManageController {

    @Resource
    private ISysMessageService sysMessageService;


    @ApiOperation(value = "保存消息配置信息", notes = "保存消息配置信息", response = SysMessageConfig.class)
    @PostMapping("/saveMessageConfig")
    public AjaxResult saveMessageConfig(@RequestBody SysMessageConfig config) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.saveMessageConfig(config));
    }

    @ApiOperation(value = "查询消息配置", notes = "查询消息配置", response = SysMessageConfig.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "通知方式类型", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "perPage", value = "每页条数", required = false, dataType = "int", paramType = "query")
    })
    @GetMapping("/findConfigList")
    public AjaxResult<IPage<SysMessageConfig>> findConfigList(@RequestParam(value = "type", required = false) String type,
                                                              @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                              @RequestParam(value = "perPage", required = false) Integer perPage) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.findConfigList(type, currentPage, perPage));
    }

    @ApiOperation(value = "消息配置详情", notes = "查询消息配置详情", response = SysMessageConfig.class)
    @GetMapping("/configDetails")
    public AjaxResult<SysMessageConfig> configDetails(@RequestParam(value = "id") Integer id) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.configDetails(id));
    }

    @EventApi
    @ApiOperation(value = "保存事件类型配置信息", notes = "保存事件类型配置信息", response = SysMessageTypeConfig.class)
    @PostMapping("/savEventType")
    public AjaxResult savEventType(@RequestBody SysMessageTypeConfig config) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.savEventType(config));
    }

    @ApiOperation(value = "查询事件类型", notes = "查询事件类型", response = SysMessageTypeConfig.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "类型名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "perPage", value = "每页条数", required = false, dataType = "int", paramType = "query")
    })
    @GetMapping("/findEventTypeConfigList")
    public AjaxResult<IPage<SysMessageTypeConfig>> findEventTypeConfigList(@RequestParam(value = "name", required = false) String name,
                                                                           @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                                           @RequestParam(value = "perPage", required = false) Integer perPage) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.findEventTypeConfigList(name, currentPage, perPage));
    }

    @ApiOperation(value = "查询所有事件类型", notes = "查询所有事件类型", response = SysMessageTypeConfig.class)
    @GetMapping("/findEventTypeAllList")
    public AjaxResult<List<SysMessageTypeConfig>> findEventTypeAllList() {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.eventTypeList());
    }

    @EventApi
    @ApiOperation(value = "事件类型详情", notes = "查询事件类型详情", response = SysMessageTypeConfig.class)
    @GetMapping("/typeConfigDetails")
    public AjaxResult<SysMessageTypeConfig> typeConfig(@RequestParam(value = "id") Integer id) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.typeConfig(id));
    }

    @ApiOperation(value = "批量删除事件类型配置", notes = "批量删除事件类型配置", response = SysMessageEventListVo.class)
    @GetMapping("/deleteMessageEventType")
    public AjaxResult deleteMessageEventType(@RequestParam("Ids") List<Integer> Ids) {
        int de = sysMessageService.deleteMessageEventType(Ids);
        if (de == -1) {
            return new AjaxResult(WamingConstant.FAILURE, "删除事件类型失败;事件管理有使用该类型！", null);
        }
        return new AjaxResult(WamingConstant.SUCCESS, "删除成功", null);
    }

    @EventApi
    @ApiOperation(value = "事件管理新增修改", notes = "事件管理新增修改", response = SysMessageEventVo.class)
    @PostMapping("/saveEvenManage")
    public AjaxResult saveEvenManage(@RequestBody SysMessageEventVo vo) {
        int add = sysMessageService.saveEvenManage(vo);
        if (add == -1) {
            return new AjaxResult(WamingConstant.FAILURE, "编辑事件管理失败！未找到事件信息", null);
        } else if (add == -2) {
            return new AjaxResult(WamingConstant.FAILURE, "通知信息不能为空！", null);
        }

        return new AjaxResult(WamingConstant.SUCCESS, "新增/编辑成功！", add);
    }

    @ApiOperation(value = "事件管理批量修改状态", notes = "事件管理批量修改状态", response = SysMessageEventVo.class)
    @PostMapping("/updataEvenManageStatus")
    public AjaxResult updataEvenManageStatus(@RequestBody List<SysMessageEvent> voList) {
        int add = sysMessageService.updataEvenManageStatus(voList);
        if (add == -1) {
            return new AjaxResult(WamingConstant.FAILURE, "编辑事件管理失败！未找到事件信息", null);
        }
        return new AjaxResult(WamingConstant.SUCCESS, "新增/编辑成功！", add);
    }

    @ApiOperation(value = "查询事件管理列表", notes = "查询事件管理列表", response = SysMessageEventListVo.class)
    @PostMapping("/findMessageEventList")
    public AjaxResult<IPage<SysMessageEventListVo>> findMessageEventList(@RequestBody SysMessageEventDto dto) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.findMessageEventList(dto));
    }

    @ApiOperation(value = "事件类型详情", notes = "查询事件类型详情", response = SysMessageEventListVo.class)
    @GetMapping("/messageEventDetails")
    public AjaxResult<SysMessageEventListVo> messageEventDetails(@RequestParam(value = "id") Integer id) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.messageEventDetails(id));
    }

    @ApiOperation(value = "批量删除事件配置", notes = "删除事件配置", response = SysMessageEventListVo.class)
    @GetMapping("/deleteMessageEventList")
    public AjaxResult deleteMessageEventList(@RequestParam("Ids") List<Integer> Ids) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.deleteMessageEventList(Ids));
    }

    @EventApi
    @ApiOperation(value = "根据事件类型查询事件", notes = "根据事件类型查询事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "事件类型ID",
                    dataTypeClass = Integer.class, paramType = "path", defaultValue = "12"),
    })
    @GetMapping("/type/event/{typeId}")
    public AjaxResult deleteMessageEventList(@PathVariable("typeId") Integer typeId) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.findEventByTypeId(typeId));
    }

    @EventApi
    @ApiOperation(value = "查询事件规则计算字段", notes = "查询事件规则计算字段", response = OptionNode.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventCode", value = "事件编号",
                    dataTypeClass = String.class, paramType = "path", defaultValue = "E1241"),
    })
    @PostMapping("/event/cal/fields/{eventCode}")
    public AjaxResult<List<OptionNode>> findCalFields(@PathVariable("eventCode") String eventCode) {
        List<OptionNode> nodeList = sysMessageService.findCalFields(eventCode);
        return new AjaxResult(WamingConstant.SUCCESS, null, nodeList);
    }

    @ApiOperation(value = "批量删除通知方式配置", notes = "批量删除事件类型配置", response = SysMessageEventListVo.class)
    @GetMapping("/deleteMessageConfig")
    public AjaxResult deleteMessageConfig(@RequestParam("Ids") List<Integer> Ids) {
        return new AjaxResult(WamingConstant.SUCCESS, null, sysMessageService.deleteMessageConfig(Ids));
    }

    @EventApi
    @ApiOperation(value = "事件规则保存接口", notes = "事件规则保存接口")
    @PostMapping("/event/rule/save")
    public AjaxResult<SysMessageEventListVo> saveEventRule(@Validated @RequestBody SysMessageEventSaveRule saveRule) {
        sysMessageService.saveEventRule(saveRule);
        return new AjaxResult(WamingConstant.SUCCESS, null, "");
    }

    @EventApi
    @ApiOperation(value = "查询事件规则详情", notes = "查询事件规则详情", response = SysMessageEventSaveRule.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventCode", value = "事件编号",
                    dataTypeClass = String.class, paramType = "path", defaultValue = "E1241"),
    })
    @PostMapping("/event/rule/detail/{eventCode}")
    public AjaxResult<SysMessageEventSaveRule> findEventRuleDetail(@PathVariable("eventCode") String eventCode) {
        SysMessageEventSaveRule saveRule = sysMessageService.findEventRuleDetail(eventCode);
        return new AjaxResult(WamingConstant.SUCCESS, null, saveRule);
    }
}

